package form;

import java.awt.FlowLayout;
import connection.ConnectionManager;
import connection.HostManager;

public class RequestDrawDialog extends javax.swing.JDialog {

    //public RequestDrawDialog(java.awt.Frame parent, boolean modal) {
    public RequestDrawDialog(java.awt.Frame parent, boolean modal, ConnectionManager connMan, HostManager hostMan) {
        super(parent, modal);
        setSize(225, 64);
        setTitle("Opponent requests draw");
        setLayout(new FlowLayout());
        initComponents();
        mainFrame = (MainFrame) parent;
        RequestDrawDialog.connMan = connMan;
        RequestDrawDialog.hostMan = hostMan;
    }

    private void initComponents() {
        okButton = new javax.swing.JButton();
        denyButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        okButton.setMnemonic('O');
        okButton.setText("Ok");
        okButton.setName("okButton"); 
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        add(okButton);
        denyButton.setMnemonic('D');
        denyButton.setText("Deny");
        denyButton.setName("denyButton");
        denyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                denyButtonActionPerformed(evt);
            }
        });
        add(denyButton);
    }
        	
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	if (hostMan != null)
    		hostMan.sendData("3:");
    	else if (connMan != null)
    		connMan.sendData("3:");
    	dispose();
    	mainFrame.drawAccepted();
    }

    private void denyButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	if (hostMan != null)
    		hostMan.sendData("4:");
    	else if (connMan != null)
    		connMan.sendData("4:");
    	dispose();
    }

    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RequestDrawDialog dialog = new RequestDrawDialog(new javax.swing.JFrame(), true, connMan, hostMan);
                //RequestDrawDialog dialog = new RequestDrawDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                    	if (hostMan != null)
                    		hostMan.sendData("4:");
                    	else if (connMan != null)
                    		connMan.sendData("4:");
                        System.exit(0);//yeah, I'll take that as a denial for a draw
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
	private javax.swing.JButton okButton;
    private javax.swing.JButton denyButton;
    private static HostManager hostMan;
    private MainFrame mainFrame;
    private static ConnectionManager connMan;
}
